package com.jrx.anytxn.accounting.job.glinterfaceInvestor.step;

import com.jrx.anytxn.accounting.bean.GlInterfaceInvestorBean;
import com.jrx.anytxn.accounting.entity.GaVoucherInfo;
import com.jrx.anytxn.accounting.entity.TlGlInterfaceInvestorInfoExcp;
import com.jrx.anytxn.accounting.mapper.ext.ExtTlGlInterfaceInvestorInfoExcpMapper;
import com.jrx.anytxn.accounting.mapper.ext.ExtVoucherInfoMapper;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 总账流水写处理
 **/
@Service
public class GlInterfaceInvestorWriter implements ItemWriter<GlInterfaceInvestorBean> {

    @Resource
    private ExtVoucherInfoMapper extVoucherInfoMapper;
    @Resource
    private ExtTlGlInterfaceInvestorInfoExcpMapper extTlGlInterfaceInvestorInfoExcpMapper;

    @Override
    public void write(List items) {

        List<GaVoucherInfo> voucherInfoList = new ArrayList<>();
        List<TlGlInterfaceInvestorInfoExcp> tlGlInterfaceInvestorInfoExcpArrayList = new ArrayList<>();

        for (Object item : items) {
            GlInterfaceInvestorBean bean = (GlInterfaceInvestorBean) item;
            if (!CollectionUtils.isEmpty(bean.getGaVoucherInfoList())) {
                voucherInfoList.addAll(bean.getGaVoucherInfoList());
            }
            if (!CollectionUtils.isEmpty(bean.getTlGlInterfaceInvestorInfoExcpList())) {
                tlGlInterfaceInvestorInfoExcpArrayList.addAll(bean.getTlGlInterfaceInvestorInfoExcpList());
        }
    }

        if (!CollectionUtils.isEmpty(voucherInfoList)) {
            extVoucherInfoMapper.batchInsert(voucherInfoList);
        }

        if (!CollectionUtils.isEmpty(tlGlInterfaceInvestorInfoExcpArrayList)) {
            extTlGlInterfaceInvestorInfoExcpMapper.batchInsert(tlGlInterfaceInvestorInfoExcpArrayList);
        }
    }

}



